-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MenuItem][Base] Pass idGenerator function #37364
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
Netlify deploy previewhttps://deploy-preview-37364--material-ui.netlify.app/ Bundle size report |
useCompoundItem
MenuItem
fbb1954
to
630f718
Compare
630f718
to
bcdfd0b
Compare
MenuItem
); | ||
} | ||
if (typeof id === 'function') { | ||
providedOrGeneratedId = (id as MissingKeyGenerator<Key>)(subitemKeys.current); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically TS is correct because Key
can be a function type. But I think we can leave it as is. I don't expect anyone to create a key that's a function.
@@ -55,13 +57,13 @@ export function useCompoundItem<Key, Subitem>( | |||
} | |||
|
|||
const { registerItem } = context; | |||
const [registeredId, setRegisteredId] = React.useState(id); | |||
const [registeredId, setRegisteredId] = React.useState(typeof id === 'function' ? undefined : id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since parameter to pass to the id
function is not available at this point, i initiated value of registeredId
to undefined
if id
is function.
@@ -11,6 +11,10 @@ import { | |||
import { useListItem } from '../useList'; | |||
import { useCompoundItem } from '../utils/useCompoundItem'; | |||
|
|||
function idGenerator(existingKeys: Set<string>) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really sure how to test that if this function is being called or not as this function runs only on react versions less than 18.
@@ -12,22 +12,22 @@ interface RegisterItemReturnValue<Key> { | |||
deregister: () => void; | |||
} | |||
|
|||
export type MissingKeyGenerator<Key> = (existingKeys: Set<Key>) => Key; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just call it KeyGenerator
or KeyProvider
*/ | ||
registerItem: ( | ||
id: Key | undefined, | ||
id: Key | MissingKeyGenerator<Key> | undefined, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think undefined
is no longer a valid option
); | ||
} | ||
if (typeof id === 'function') { | ||
providedOrGeneratedId = (id as MissingKeyGenerator<Key>)(subitemKeys.current); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically TS is correct because Key
can be a function type. But I think we can leave it as is. I don't expect anyone to create a key that's a function.
@@ -32,18 +36,16 @@ export interface UseCompoundItemReturnValue<Key> { | |||
* @ignore - internal hook. | |||
*/ | |||
export function useCompoundItem<Key, Subitem>( | |||
id: Key | undefined, | |||
id: Key | MissingKeyGenerator<Key> | undefined, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above, I think undefined
can be dropped from here.
let providedOrGeneratedId: Key; | ||
if (id === undefined) { | ||
if (missingKeyGenerator === undefined) { | ||
throw new Error( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since id
cannot be undefined, i don't think throwing error makes much sense
@michaldudak done with all changes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Looks good!
closes: #37251
(open menu in before sandbox to see the error)
before: https://codesandbox.io/s/falling-hooks-4tbj3g?file=/package.json
after: https://codesandbox.io/s/serverless-morning-xf15xb?file=/index.tsx